#include "PlayTurnsMenu.h"
#include "Utilities.h"
#include "PrintUtilities.h"
#include "User.h"
#include "League.h"
#include "LeagueManager.h"
#include "MenuManager.h"
#include "Global.h"
#include "Player.h"
#include "GamePrintUtils.h"

#include <algorithm>

#define ALL_MATCH_LOG

PlayTurnsMenu::PlayTurnsMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
{
}

void PlayTurnsMenu::Draw() const
{
	const LeagueVec& leagues = GetSP()->GetLeagueMgr().GetLeagues(true);

	for (size_t i = 0; i < leagues.size(); ++i)
	{
		MatchVec currentTurn = leagues[i]->GetLastPlayedTurn();

		MatchVecIt begin = currentTurn.begin();
		MatchVecIt end = currentTurn.end();
		
		std::cout << std::endl;
		std::string playersEvents = "";
		if (! (GetSP()->GetLeagueMgr().GetCurrentTurn() > leagues[i]->GetCalendar()->GetTurnNumber()))
		for (MatchVecIt It = begin; It != end; ++It)
		{
			const Match& match = *It;
			PrintMatch(std::cout, match);

			for(int j=0; j<2; ++j)
			{
				const PlayerVec& playerList = match.GetTeam(j)->GetPlayersList();

				for (size_t i = 0; i < Team::GetParams().TITOLARS_PLAYERS; ++i)
				{
					if (playerList[i]->IsBroken())
					{

#ifdef ALL_MATCH_LOG
						std::cout <<" ! " << match.GetTeam(j)->GetName() << ": " + playerList[i]->GetName() << " infortunato per " <<
											Utilities::IntToString(playerList[i]->GetDaysUnavaiable()) << " giornate.\n";

#else
						if (m.m_team[j]->IsUserControlled())
							playersEvents += " ! " + match.m_team[j]->GetName() + ": " + playerList[i]->GetName() + " infortunato per " +
											Utilities::IntToString(playerList[i]->GetDaysUnavaiable()) + " giornate.\n";

#endif
					}
				}
			}
				//std::cout << std::endl;
		}
		
	#ifndef ALL_MATCH_LOG
		
		/*if (verbose && !playersEvents.empty())
			std::cout << "\n EVENTI NOTEVOLI DELLA GIORNATA:\n" + playersEvents;*/

	#endif

		std::cout << std::endl;
	}
	std::cout	<< Print::Options(" 0) Return to previous menu'")
				<< " Selection: ";
}


bool PlayTurnsMenu::Input(const std::string& i_input)
{
	bool goodInput = true;

	int res = Utilities::StringToInt(i_input);

	if (res == 0)
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	else 
		goodInput = false;
	

	return goodInput;
}